package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.*;
import com.woniuxy.factory.mapper.DepMapper;
import com.woniuxy.factory.mapper.EmpMapper;
import com.woniuxy.factory.mapper.PersonnelRecordMapper;
import com.woniuxy.factory.service.EmpService;
import com.woniuxy.factory.util.ApplicationType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @NTLX data 2020 08 19
 */
@Service
public class EmpServiceImp implements EmpService {

    @Autowired
    EmpMapper empMapper;
    @Autowired
    DepMapper depMapper;
    @Autowired
    PersonnelRecordMapper recordMapper;

    @Override
    public int selectCountByDep(Integer depNum) {
        return empMapper.selectCountByDep(depNum);
    }

    @Override
    public Emp selectDepLeader(Integer depNum, Integer post) {
        return null;
    }

    @Override
    public int updateEmpPost(Emp emp) {
        return 0;
    }

    @Override
    public int updateEmpDep(Emp emp) {
        return 0;
    }

    @Override
    public List<Emp> selectEmp(Emp emp) {
        return empMapper.selectEmpByCondition(emp);
    }

    @Override
    public List<Emp> selectEmp(Emp emp, String depName) {
        Dep dep;
        if((dep = depMapper.queryDepByName(depName))==null){
            return null;
        }else {
            emp.setDep(dep);
            return empMapper.selectEmpByCondition(emp);
        }
    }

    @Override
    public void transferEmp(Emp emp, Dep afterDep, Post afterPost) {
        //新建调动记录
        PersonnelRecord record = new PersonnelRecord(emp,3,emp.getDep(),emp.getPost(),afterDep,afterPost,new Date());
        System.out.println(record.getEmp().getEmpId());
        //插入调动记录
        if(recordMapper.insertTransferRecord(record)==0){
            //插入语句返回值为零插入失败
            throw new RuntimeException("调动记录插入失败");
        }
        //将新的部门岗位信息赋值给员工
        emp.setDep(afterDep);
        emp.setPost(afterPost);
        //修改员工信息
        if(empMapper.updateEmpInfo(emp)==0){
            throw new RuntimeException("修改员工信息失败");
        }
    }

    @Override
    public ResponseResult getEmps(Emp emp) {
        Dep dep = new Dep();
        dep.setDepNum(ApplicationType.PURCHASEQUEST);
        emp.setDep(dep);
        List<Emp> emps=empMapper.selectSinpleEmpByCondition(emp);
        return new ResponseResult(0,"查询成功",null,emps);
    }


}
